/*

Input: newdata/estimation_prep_ltw18 [prepared precinct-level panel]

Output: Figure C.8, C.9, C.10, C.11, Table C.7

Tasks: Robustness to different estimators and continuout treatments 
	
	
*/	
	
* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear
	
	
********************************************************************************
		// Prep Estimation //
********************************************************************************

	// compute group ids for DISTANCE increase/decrease, 0 else in K
	//	> first election w 100% of addresses reassigned
	gen 	tmp = (del_street_dist>0) 			if K==0
	bys sb_new (tmp): gen ind_dist_up = tmp[1]
	replace ind_dist_up = 0 					if missing(Ei)
	lab var ind_dist_up "=1 if dist increase in event (Ei), 0 else"
	
	cap drop tmp*
	gen 	tmp = (del_street_dist<0) 			if K==0
	bys sb_new (tmp): gen ind_dist_dn = tmp[1]
	replace ind_dist_dn = 0 					if missing(Ei)
	lab var ind_dist_dn "=1 if dist decrease in event (Ei), 0 else"	
	
	// compute group ids for DISTANCE increase/decrease, 0 else in K50
	//	> first election w 50%+ reass.
	cap drop tmp*
	gen 	tmp = (del_street_dist>0) 			if K50==0
	bys sb_new (tmp): gen ind_dist_up50 = tmp[1]
	replace ind_dist_up50 = 0 					if missing(Ei50)
	lab var ind_dist_up50 "=1 if dist increase in event (Ei50), 0 else"
	
	cap drop tmp*
	gen 	tmp = (del_street_dist<0) 			if K50==0
	bys sb_new (tmp): gen ind_dist_dn50 = tmp[1]
	replace ind_dist_dn50 = 0 					if missing(Ei50)
	lab var ind_dist_dn50 "=1 if dist decrease in event (Ei50), 0 else"		

	// compute id for DISTANCE increase/decrease, 0 else in K_max 
	//	> election with highest share of reassignments
	cap drop tmp*
	gen 	tmp = (del_street_dist>0) 			if K_max==0
	bys sb_new (tmp): gen ind_dist_upX = tmp[1]
	replace ind_dist_upX = 0 					if missing(Ei_max)
	lab var ind_dist_upX "=1 if dist increase in event, 0 else"
	
	cap drop tmp*
	gen 	tmp = (del_street_dist<0) 			if K_max==0
	bys sb_new (tmp): gen ind_dist_dnX = tmp[1]
	replace ind_dist_dnX = 0 					if missing(Ei_max)
	lab var ind_dist_dnX "=1 if dist decrease in event, 0 else"	
	
	// gen del_street_dist_max := ABSOLUTE value of delta dist in K_max=0;
	// gen del_street_dist_Kmax:= actual value in K_max=0
	//		-> cont. across time.
	gen del_street_dist_max=del_street_dist if K_max==0
	grconst del_street_dist_max, by(sb_new) fill
	replace del_street_dist_max=0 if Ei_max ==.
	
	gen 	del_street_dist_Kmax = del_street_dist_max
	replace del_street_dist_max = del_street_dist_max*(-1) if del_street_dist_max<0
		
	//  del_street_dist_const := change in K==0
		gen del_street_dist_const = del_street_dist if K==0
		bys sb_new (del_street_dist_const): replace del_street_dist_const=del_street_dist_const[1]	//time constant 
		replace del_street_dist_const=0 if missing(K)	
		lab var del_street_dist_const "change in street dist in K=0, NT=0"	
	
	// del_street_dist_const_abs := ABSOLUTE value in K=0
		gen del_street_dist_const_abs = abs(del_street_dist_const)
		lab var del_street_dist_const_abs "abs(change in street dist in K=0), NT=0"		
	
* gen leads and lags
	
 * TWFE OLS; Event = 100% reassg.
	// gen leads and lags
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	order F1event, last
	
 * TWFE OLS; Event = 50%+ reassg.
	// gen leads and lags
	forvalues l = 7(-1)1 {
		gen F`l'event50 = K50==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event50 = K50==`l'
	}
	order F1event50, last	
	
	
* Rescale ES Dummies; Event = Election where STRONGEST reassgn. happend.	
	// gen leads and lags: Event = LARGEST  reassg.
	forvalues l = 7(-1)1 {
		gen F`l'eventX = K_max==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'eventX = K_max==`l'
	}
	order F1eventX, last	
	
	// rescale by treatment intensity
	gen treat_simple_max = treat_simple if K_max==0 
	grconst treat_simple_max, by(sb_new) fill 
	replace treat_simple_max=0 if Ei_max==.
	
	forvalues l = 7(-1)1 {
		gen F`l'eventRS = F`l'eventX
		replace F`l'eventRS=F`l'eventRS*treat_simple_max
	}	
	forvalues l = 0/7 {
		gen L`l'eventRS = L`l'eventX
		replace L`l'eventRS= L`l'eventRS*treat_simple_max
	}
	order F1eventRS, last
	
	cap drop tmp*
 * Prep for estimation with did_multiplegt of de Chaisemartin and D'Haultfoeuille (2020)
	
	// treat_simple_clean := treat_simple in election with HIGHEST reassng. shock (% reass); zero before;
	cap drop treat_simple_clean
	gen 	treat_simple_clean = 0 if K_max!=0
	bys sb_new: ereplace treat_simple_clean = max(treat_simple) if K_max>=0 & !missing(Ei_max)
	
	assert treat_simple_clean==0 if Ei_max==.	
	
	// gen del_street_dist_clean:= 0 pre-treatment, ABS(del_street_dist) posttreatment
	gen del_street_dist_clean = 0 if K_max!=0
	bys sb_new: replace del_street_dist_clean = del_street_dist_max if K_max>=0 & !missing(Ei_max)
	assert del_street_dist_clean==0 if Ei_max==.	
	lab var del_street_dist_clean "0 preT, abs(Δdist) postT, K_max"
	
	// gen del_street_dist_clean2:= 0 pre-treatment, del_street_dist (pos/neg) posttreatment
	gen     del_street_dist_clean2=del_street_dist_Kmax
	replace del_street_dist_clean2=0 if K_max<0
	lab var del_street_dist_clean2 "0 preT, Δdist postT, K_max"
	
 * Rolling treatment: treatment dose (change in dist) as of last election with reassignment 	
	// del_street_dist_roll := Δdist as of last election with reass. 
	cap drop del_street_dist_roll
	gen del_street_dist_roll = del_street_dist
	*bys sb_new (wahl_id): replace del_street_dist_roll = del_street_dist_roll[_n-1] if abs(del_street_dist_roll[_n-1]) > abs(del_street_dist_roll[_n]) & del_street_dist_roll[_n-1]!=.	
	bys sb_new (wahl_id): replace del_street_dist_roll = del_street_dist_roll[_n-1] if del_street_dist_roll[_n-1] !=0 & del_street_dist_roll[_n-1]!=. & del_street_dist_roll[_n] ==0	
	lab var del_street_dist_roll "Δdist as of last election with reassgn."
	
	// del_street_dist_roll := Δdist as of last election with reass. 
	cap drop del_street_dist_cum
	gen del_street_dist_cum = del_street_dist
	bys sb_new (wahl_id): replace del_street_dist_cum = del_street_dist_cum[_n-1]+ del_street_dist_cum if del_street_dist_cum[_n-1] !=.	
	
	// election FE x treatment in first election: ctf* 
	cap drop D1
	bys sb_new (wahl_id): gen D1=treat_simple_clean[1]
	
	cap drop ctf* 
	tab1 wahl_id, gen(ctf)
	foreach v of varlist ctf* {
		replace `v' = `v' * D1
	}
	// polynomial_ ctf_2*
	tab1 wahl_id, gen(ctf_2)
	foreach v of varlist ctf_2* {
		replace `v' = `v' * D1^2
	}

	// election FE x treatment in first election: ctf* 
	cap drop D1
	bys sb_new (wahl_id): gen D1=del_street_dist_roll[1]
	
	cap drop dctf* 
	tab1 wahl_id, gen(dctf)
	foreach v of varlist dctf* {
		replace `v' = `v' * D1
	}

 	
*** TABLE C.7: Sample Composition by Treatment Definition
cap frame drop tablec7
	frame create tablec7 str60 tdef int treated int untreated	

  * binary treatment, 100% addresses reassigned
	distinct sb_new if Ei!=.	// 	treated: 280
	local unt = r(ndistinct)
	distinct sb_new if Ei==. 	// 	untreated:   338
	frame post tablec7 ("binary treatment, 100% reassigned") (`unt') (`r(ndistinct)')	
  * binary treatment, 50%+ addresses reassigned
	distinct sb_new if Ei50!=.	// 	treated: 372
	local unt = r(ndistinct)
	distinct sb_new if Ei50==. 	// 	untreated:   246
	frame post tablec7 ("binary treatment, 50%+ reassigned") (`unt') (`r(ndistinct)')
  * continuous treatment, (% addresses reassigned or Δdistance)
	distinct sb_new if Ei_max!=.	// 	treated: 513
	local unt = r(ndistinct)
	distinct sb_new if Ei_max==. 	// 	untreated:   105
	frame post tablec7 ("continuous treatment, (% addresses reassigned or Δdistance)") (`unt') (`r(ndistinct)')	
	frame tablec7: list,  table notrim
	frame tablec7: texsave using "$tables/Table_C7_Smpl_comp_by_treat_def.tex", ///
						align(lcc) hlines(1 -1) frag replace noendash
						
********************************************************************************
// Binary T (100%), binary T (50%+), cont. T (TWFE), cont. T (dC et al, 2023) //
// -> Pooled Reassignments (Figure C8) 
********************************************************************************		

	// Estimate baseline ES
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		  reghdfe `v' F7event-L7event F1event $ctr $wgt  if smpl_trim==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		
		estimates store `v'_bsl
	}

	// 50%+ reassignments														
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		 reghdfe `v' F7event50-L7event50 F1event50 $ctr $wgt if smpl_trim50==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		
		estimates store `v'_50
	}	
	
	// Bsl specification with rescaled ES dummies 								
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		 reghdfe `v' F7eventRS-L7eventRS F1eventRS $ctr $wgt , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		
		estimates store `v'_rs
	}	

	// dCdH (2023):  CONT TREATMENT
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		did_multiplegt_dyn `v' sb_new wahl_id  treat_simple_clean ,  placebo(3) effects(3)   cluster(sb_new)  controls(ctf* $ctr) weight(wahlber_gesamt) graph_off
		
		// storing the estimates
		matrix dcdh_b_`v' = e(estimates) 
		matrix dcdh_v_`v' = e(variances)	
		matrix  rownames dcdh_b_`v' =Effect_0 Effect_1 Effect_2 Av_tot_eff Placebo_2 Placebo_3 Placebo_4 Placebo_1
		matrix  rownames dcdh_v_`v' =Effect_0 Effect_1 Effect_2 Av_tot_eff Placebo_2 Placebo_3 Placebo_4 Placebo_1	
	}	
	
** PLOT 	
	// legend
	local legd `"order(1 "Binary treatment: full reassignments (Baseline)" 3 "Binary treatment: 50%+ addresses reassigned" 5 "Continuous treatment: rescaled event-time dummies" 7 "Continuous treatment (deChaisemartin et al, 2023)" )"'
	
	// PLOT: Turnouts Urne 
	local d = "turnout_urne" 
	event_plot   `d'_bsl `d'_50 `d'_rs dcdh_b_`d'#dcdh_v_`d', ///
	stub_lag(L#event L#event50 L#eventRS Effect_#) stub_lead(F#event F#event50 F#eventRS Placebo_#) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `legd' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel A.} Effect on Polling Place Turnout",nobox span bexpand justification(left) size(medium)) ///
		name(urne, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry))  ///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green))
	
	// PLOT: Turnouts Postal	
	local d = "turnout_pos_req" 
	event_plot   `d'_bsl `d'_50 `d'_rs dcdh_b_`d'#dcdh_v_`d' , ///
	stub_lag(L#event L#event50 L#eventRS Effect_#) stub_lead(F#event F#event50 F#eventRS Placebo_#) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `legd' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel B.} Effect on Mail-in Turnout",nobox span bexpand justification(left) size(medium)) ///
		name(postal, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry))  ///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green))

	// PLOT: Turnouts Total
	local d = "turnout_tot_req" 
	event_plot   `d'_bsl `d'_50 `d'_rs dcdh_b_`d'#dcdh_v_`d', ///
	stub_lag(L#event L#event50 L#eventRS Effect_#) stub_lead(F#event F#event50 F#eventRS Placebo_#) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `legd' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel C.} Effect on Total Turnout",nobox span bexpand justification(left) size(medium)) ///
		name(total, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry))  ///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green))
	
		
		* PLOT: FIGURE C8. Robustness to Alternative Treatment Definitions–Pooled Reassignments
		grc1leg2  urne postal total , xcommon  col(2) iscale(.7) name(bsl_rob_alt, replace) ///
		pos(4) ring(0) lcol(1) lxoffset(0) lyoffset(15) legscale(*.9) 
		gr_edit .legend.title.DragBy 0 -15
		graph export "$figures/Figure_C8_ES_alternative_treatm_bsl.pdf", replace

		
********************************************************************************
// Binary T (100%), binary T (50%+), cont. T (TWFE), cont. T (dC et al, 2023)  //
// -> Reassignments by distance increase/decrease (Figure C9)
********************************************************************************		

	// Create two set of dummies: Reason Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_dist_dn				// a := decrease
		lab var F`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b = F`l'event *ind_dist_up				// b:= increase
		lab var F`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"

		assert  F`l'event_b+F`l'event_a==F`l'event		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_dist_dn	// a := decrease
		lab var L`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b = L`l'event *ind_dist_up	// b:= increase
		lab var L`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		assert  L`l'event_b+L`l'event_a==L`l'event
	}		
	
	// ORDER dummies
	order *event_b, last
	order F1event*,last		

* ES dummies for treat=50%+
	// Create two set of dummies: Dist Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a50 = F`l'event50 *ind_dist_dn50				// a := decrease
		lab var F`l'event_a50 "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b50 = F`l'event50 *ind_dist_up50				// b:= increase
		lab var F`l'event_b50 "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"

		assert  F`l'event_b50+F`l'event_a50==F`l'event50		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a50 = L`l'event50 *ind_dist_dn50	// a := decrease
		lab var L`l'event_a50 "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b50 = L`l'event50 *ind_dist_up50	// b:= increase
		lab var L`l'event_b50 "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		assert  L`l'event_b50+L`l'event_a50==L`l'event50
	}		
	
	// ORDER dummies
	order *event_b50, last
	order F1event50*,last				
	
* Rescaled dummies
	// Create two set of dummies: Reason Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_aX = F`l'eventX *ind_dist_dnX	 *treat_simple_max //*del_street_dist_max	// a := decrease
		lab var F`l'event_aX "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_bX = F`l'eventX *ind_dist_upX	 *treat_simple_max //*del_street_dist_max	// b:= increase
		lab var F`l'event_bX "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"
		*assert  F`l'event_bX+F`l'event_aX==F`l'eventX
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_aX = L`l'eventX *ind_dist_dnX	 *treat_simple_max //*del_street_dist_max // a := decrease
		lab var L`l'event_aX "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_bX = L`l'eventX *ind_dist_upX *treat_simple_max //*del_street_dist_max	// b:= increase
		lab var L`l'event_bX "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		*assert  L`l'event_bX+L`l'event_aX==L`l'eventX
	}		
	
	// ORDER dummies
	order *event_bX, last
	order F1event*,last	
	
	
	// Estimate ES by increase/decrease in distance
	estimates clear
	

	// Baseline
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		 reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b $ctr $wgt ///
					if  smpl_trim==1  , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_a_bsl
		estimates store `v'_b_bsl
	}	
		
	
	// 50%+ treatment															
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		reghdfe `v' F7event_a50-L7event_a50  F7event_b50-L7event_b50 F1event_a50 F1event_b50 $ctr $wgt ///
					if  smpl_trim50==1 , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_a_50
		estimates store `v'_b_50
	}	
	
	// Rescaled dummies
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
	 reghdfe `v' F7event_aX-L7event_aX  F7event_bX-L7event_bX F1event_aX F1event_bX $ctr $wgt ///
					 , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_a_rs
		estimates store `v'_b_rs	
	}		
	
	// dCdH (2023):  CONT TREATMENT
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		// dist increase
		did_multiplegt_dyn `v' sb_new wahl_id  treat_simple_clean if del_street_dist_Kmax>=0,  ///
			placebo(3) effects(3)  cluster(sb_new)  controls($ctr) weight(wahlber_gesamt)	graph_off
		// storing the estimates
		matrix dcdh_b_up`v' = e(estimates) 
		matrix dcdh_v_up`v' = e(variances)	
		matrix  rownames dcdh_b_up`v' =Effect_0 Effect_1 Effect_2 Av_tot_eff Placebo_2 Placebo_3 Placebo_4 Placebo_1
		matrix  rownames dcdh_v_up`v' =Effect_0 Effect_1 Effect_2 Av_tot_eff Placebo_2 Placebo_3 Placebo_4 Placebo_1	
		
		// dist decrease
		did_multiplegt_dyn `v' sb_new wahl_id   treat_simple_clean if del_street_dist_Kmax<=0, ///
			placebo(3) effects(3)  cluster(sb_new)  controls($ctr) weight(wahlber_gesamt)	graph_off	
		// storing the estimates
		matrix dcdh_b_dn`v' = e(estimates) 
		matrix dcdh_v_dn`v' = e(variances)	
		matrix  rownames dcdh_b_dn`v' =Effect_0 Effect_1 Effect_2 Av_tot_eff Placebo_2 Placebo_3 Placebo_4 Placebo_1
		matrix  rownames dcdh_v_dn`v' =Effect_0 Effect_1 Effect_2 Av_tot_eff Placebo_2 Placebo_3 Placebo_4 Placebo_1	
	}	
			
	
** PLOT 	
	// legend
	local legd `"order(1 "Binary treatment: full reassignments (baseline)" 3 "Binary treatment: 50%+ addresses reassigned" 5 "Continuous treatment: rescaled event-time dummies" 7 "Continuous treatment (deChaisemartin et al , 2023)" )"'	
	
 foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		
		// PLOT Dist Decreas
		event_plot  `v'_a_bsl `v'_a_50 `v'_a_rs dcdh_b_dn`v'#dcdh_v_dn`v', ///
		stub_lag(L#event_a L#event_a50 L#event_aX Effect_#) stub_lead(F#event_a F#event_a50 F#event_aX Placebo_#) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `legd' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		subtitle("{bf:b.} Distance decrease",nobox justification(left) size(medsmall)) ///
		name(`v'_dwn, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry))  ///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green)) 
	
		// PLOT Dist Increase
		event_plot  `v'_b_bsl `v'_b_50 `v'_b_rs dcdh_b_up`v'#dcdh_v_up`v' , ///
		stub_lag(L#event_b L#event_b50 L#event_bX Effect_#) stub_lead(F#event_b F#event_b50 F#event_bX Placebo_#) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `legd' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		subtitle("{bf:a.} Distance increase",nobox justification(left) size(medsmall)) ///
		name(`v'_up, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry))  ///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green))	
	
	
 }	
 
	* PLOT: FIGURE C9. Robustness to Alternative Treatment Definitions–Effects by Distance Change
	grc1leg2 turnout_urne_up turnout_urne_dwn, 		 name(g1, replace) ///
			title("{bf:Panel A.} Effect on Polling Place Turnout", just(left) bexpand size(small)) loff  iscale(.7) imargins(small)
	grc1leg2 turnout_pos_req_up turnout_pos_req_dwn, name(g2, replace) ///
			title("{bf:Panel B.} Effect on Mail-in Turnout", just(left) bexpand size(small)) loff  iscale(.7) imargins(small)
	grc1leg2 turnout_tot_req_up turnout_tot_req_dwn, name(g3, replace) ///
			title("{bf:Panel C.} Effect on Total Turnout", just(left) bexpand size(small))   iscale(.7) imargins(small)
	grc1leg2 g1 g2 g3, col(1) imargins(zero) legscale(*.65) lrow(2)
	gr_edit .style.editstyle declared_ysize(6) editcopy	
	graph export "$figures/Figure_C9_ES_alternative_treat_dist2.pdf", replace 				
	
	
********************************************************************************
// deChaisemartin et al, 2023 estimator: cont treatment based on dist change (Fig C11) //
********************************************************************************		
	
forvalues x=1/3 {
	
	if `x'==1 {
		loc sw ""
		loc tt "{bf: Panel A.} Pooled Sample"
	}
	if `x'==2 {
		loc sw "switchers(out)"
		loc tt "{bf: Panel B.} Distance Decreases ('switchers out')"
	}	
	if `x'==3 {
		loc sw "switchers(in)"
		loc tt "{bf: Panel C.} Distance Increases ('switchers in')"		
	}	

	// dCdH (2023):  CONT TREATMENT
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		did_multiplegt_dyn `v' sb_new wahl_id  del_street_dist_cum,  placebo(3) effects(3)  cluster(sb_new)  ///
			controls($ctr  dctf* ) weight(wahlber_gesamt) graph_off drop_larger_lower `sw' 

		// storing the estimates
		matrix dcdh_b_`v'`x' = e(estimates) 
		matrix dcdh_v_`v'`x' = e(variances)	
		matrix  rownames dcdh_b_`v'`x' =Effect_0 Effect_1 Effect_2 Av_tot_eff Placebo_2 Placebo_3 Placebo_4 Placebo_1
		matrix  rownames dcdh_v_`v'`x' =Effect_0 Effect_1 Effect_2 Av_tot_eff Placebo_2 Placebo_3 Placebo_4 Placebo_1	
	}
}		


	// PLOT:  Overall Effects
	event_plot  dcdh_b_turnout_urne1#dcdh_v_turnout_urne1 dcdh_b_turnout_pos_req1#dcdh_v_turnout_pos_req1 dcdh_b_turnout_tot_req1#dcdh_v_turnout_tot_req1 , ///
	stub_lag(Effect_#  ) stub_lead(Placebo_#  ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.15(0.15)0.15) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12)order(1 "Polling place turnout" 3 "Mail-in turnout" 5 "Total turnout" ) row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf: Panel A.} Pooled Sample",nobox span bexpand justification(left) size(12pt)) ///
		name(dCdH_pool, replace)) ///
	lag_opt1(msymbol(S) msize(3pt) color(navy)) 	lag_ci_opt1(color(navy)) ///
	lag_opt2(msymbol(O) msize(3pt) color(cranberry)) 	lag_ci_opt2(color(cranberry)) ///  
	lag_opt3(msymbol(Oh) msize(3pt) color(black)) 	lag_ci_opt3(color(black))			
	
	
	// PLOT: Switchers out (decreas)
	event_plot dcdh_b_turnout_urne2#dcdh_v_turnout_urne2 dcdh_b_turnout_pos_req2#dcdh_v_turnout_pos_req2 dcdh_b_turnout_tot_req2#dcdh_v_turnout_tot_req2 , ///
	stub_lag(Effect_#  ) stub_lead(Placebo_#  ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.15(0.15)0.15) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12)order(1 "Polling place turnout" 3 "Mail-in turnout" 5 "Total turnout" ) row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf: Panel B.} Distance Decreases ('switchers out')"	,nobox span bexpand just(left) size(12pt)) ///
		name(dCdH_dwn, replace)) ///
	lag_opt1(msymbol(S) msize(3pt) color(navy)) 	lag_ci_opt1(color(navy)) ///
	lag_opt2(msymbol(O) msize(3pt) color(cranberry)) 	lag_ci_opt2(color(cranberry)) ///  
	lag_opt3(msymbol(Oh) msize(3pt) color(black)) 	lag_ci_opt3(color(black))

	
	// PLOT: Switchers in (increase)
	event_plot  dcdh_b_turnout_urne3#dcdh_v_turnout_urne3 dcdh_b_turnout_pos_req3#dcdh_v_turnout_pos_req3 dcdh_b_turnout_tot_req3#dcdh_v_turnout_tot_req3 , ///
	stub_lag(Effect_#  ) stub_lead(Placebo_#  ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.15(0.15)0.15) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12)order(1 "Polling place turnout" 3 "Mail-in turnout" 5 "Total turnout" ) row(1) region(style(none)) ) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf: Panel C.} Distance Increases ('switchers in')"	,nobox span bexpand just(left) size(12pt)) ///
		name(dCdH_up, replace)) ///
	lag_opt1(msymbol(S) msize(3pt) color(navy)) 	lag_ci_opt1(color(navy)) ///
	lag_opt2(msymbol(O) msize(3pt) color(cranberry)) 	lag_ci_opt2(color(cranberry)) ///  
	lag_opt3(msymbol(Oh) msize(3pt) color(black)) 	lag_ci_opt3(color(black))
	
	* PLOT: FIGURE C11. Robustness to Alternative Treatment Definition: Continuous Treatment and de Chaisemartin et al. (2023) estimator
	grc1leg2  dCdH_pool dCdH_dwn dCdH_up , xcommon  col(2) iscale(.7) ///
	pos(4) ring(0) lcol(1) ltitle("Outcome:")  ///
				lxoffset(-20) lyoffset(20) legscale(*.9) ltsize(*.8)
	gr_edit .legend.title.DragBy 0 -8
	graph export "$figures/Figure_C11_ES_dCdH23_dist.pdf", replace 	

	// report estimates
	mat list  dcdh_b_turnout_urne3
	mat list  dcdh_b_turnout_pos_req3



********************************************************************************
 // Triple diff, Event=largest reassignment, different dist slopes (Figure C10) //
********************************************************************************	

	
	cap drop *X_n
	// Create two set of dummies: Reason Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_aX_n = F`l'eventX *ind_dist_dnX	 *del_street_dist_max //*del_street_dist_max	// a := decrease
		lab var F`l'event_aX_n "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_bX_n = F`l'eventX *ind_dist_upX	 *del_street_dist_max //*del_street_dist_max	// b:= increase
		lab var F`l'event_bX_n "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_aX_n = L`l'eventX *ind_dist_dnX	 *del_street_dist_max //*del_street_dist_max // a := decrease
		lab var L`l'event_aX_n "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_bX_n = L`l'eventX *ind_dist_upX * del_street_dist_max //*del_street_dist_max	// b:= increase
		lab var L`l'event_bX_n "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"
	}	
	order F1event*, last
	
	// Estimate ES: base levels + interactions
	estimates clear
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		reghdfe `v' F7eventRS-L7eventRS F7event_aX_n-L7event_aX_n F7event_bX_n-L7event_bX_n F1eventRS F1event_aX_n F1event_bX_n ///
				$ctr  $wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		estimates store `v'
	}
	
	
	// PLOT:  Base levels
	event_plot  turnout_urne turnout_pos_req turnout_tot_req , ///
	stub_lag(L#eventRS ) stub_lead(F#eventRS ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.15(0.15)0.15) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment",size(medsmall)) ///
		legend(pos(12)order(1 "Polling place turnout" 3 "Mail-in turnout" 5 "Total turnout" ) row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel A.} Base Effects",nobox span bexpand justification(left) size(12pt)) ///
		subtitle("rescaled event-time dummies") ///
		name(DDD_base, replace)) ///
	lag_opt1(msymbol(S) msize(3pt) color(navy)) 	lag_ci_opt1(color(navy)) ///
	lag_opt2(msymbol(O) msize(3pt) color(cranberry)) 	lag_ci_opt2(color(cranberry)) ///  
	lag_opt3(msymbol(Oh) msize(3pt) color(black)) 	lag_ci_opt3(color(black))			
	
	// PLOT: Triple Diff: Increase
	event_plot  turnout_urne turnout_pos_req turnout_tot_req , ///
	stub_lag(L#event_bX_n ) stub_lead(F#event_bX_n ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.15(0.15)0.15) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment",size(medsmall)) ///
		legend(pos(12)order(1 "Polling place turnout" 3 "Mail-in turnout" 5 "Total turnout" ) row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel B.} Triple Difference Estimates",nobox span bexpand just(left) size(12pt)) ///
		subtitle("{&Delta}distance {c 215} event-time dummies {c 215} {bf:1}[increase]") ///
		name(DDD_up, replace)) ///
	lag_opt1(msymbol(S) msize(3pt) color(navy)) 	lag_ci_opt1(color(navy)) ///
	lag_opt2(msymbol(O) msize(3pt) color(cranberry)) 	lag_ci_opt2(color(cranberry)) ///  
	lag_opt3(msymbol(Oh) msize(3pt) color(black)) 	lag_ci_opt3(color(black))

	
	// PLOT: Triple Diff: Decrease
	event_plot  turnout_urne turnout_pos_req turnout_tot_req , ///
	stub_lag(L#event_aX_n ) stub_lead(F#event_aX_n ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.15(0.15)0.15) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment",size(medsmall)) ///
		legend(pos(12)order(1 "Polling place turnout" 3 "Mail-in turnout" 5 "Total turnout" ) row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel C.} Triple Difference Estimates",nobox span bexpand just(left) size(12pt)) ///
		subtitle(" {&Delta}distance {c 215} event-time dummies {c 215} {bf:1}[decrease]") ///
		name(DDD_dwn, replace)) ///
	lag_opt1(msymbol(S) msize(3pt) color(navy)) 	lag_ci_opt1(color(navy)) ///
	lag_opt2(msymbol(O) msize(3pt) color(cranberry)) 	lag_ci_opt2(color(cranberry)) ///  
	lag_opt3(msymbol(Oh) msize(3pt) color(black)) 	lag_ci_opt3(color(black))
	
	
	* PLOT: FIGURE C10. Triple Difference Estimates by Increase and Decrease in Distance
	grc1leg2  DDD_base DDD_up DDD_dwn  , xcommon  col(2) iscale(.7) ///
	pos(4) ring(0) lcol(1) ltitle("Outcome:")  ///
				lxoffset(-20) lyoffset(20) legscale(*.9) ltsize(*.8)
	gr_edit .legend.title.DragBy 0 -8
	graph export "$figures/Figure_C10_ES_DDD_RSevent_delta_dist_2slopes.pdf", replace 	
	
	
 *** stats for manuscript
	
	* Effect of dist INCREASE in t=0
	estimates restore turnout_urne 
	di 	as text "A 1-km distance increase changes {bf: in-person turnout} in t=0 by:" ///
		as res %12.3f _b[L0event_bX_n] as text " precentage points"	
		
	estimates restore turnout_tot_req
	di 	as text "A 1-km distance increase changes {bf: total turnout} in t=0 by:" ///
		as res %12.3f _b[L0event_bX_n] as text " precentage points"			
	
	* How much closer must the PP move to compensate turnout drop due to reassignment disutility?
	estimates restore turnout_tot_req 
	di 	as text "To offset reassignm. disutil in {bf: total turnout} in t=0, PP must move closer by:" ///
		as res %12.3f _b[ L0eventRS]/_b[L0event_aX_n] *(-1) as text " km"
	
	estimates restore turnout_urne	
	di 	as text "To offset reassignm. disutil in {bf:in-person turnout} in t=0, PP must move closer by:" ///
		as res %12.3f _b[ L0eventRS]/_b[L0event_aX_n] *(-1) as text " km"	
		
 ** Are the distance slopes statistically different for increases and decreases?
	* PP turnout
	estimates restore turnout_urne 	
	// all post periods
	lincom  (1/3)*(L0event_aX_n + L1event_aX_n + L2event_aX_n) - (1/3)*(L0event_bX_n + L1event_bX_n + L2event_bX_n)*(-1)
	// single post periods
	lincom L0event_aX_n - L0event_bX_n *(-1)
	lincom L1event_aX_n - L1event_bX_n *(-1)
	lincom L2event_aX_n - L2event_bX_n *(-1)
	
	* Mail-in turnout
	estimates restore turnout_pos_req
	// all post periods
	lincom  (1/3)*(L0event_aX_n + L1event_aX_n + L2event_aX_n) - (1/3)*(L0event_bX_n + L1event_bX_n + L2event_bX_n)*(-1)
	// single post periods
	lincom L0event_aX_n - L0event_bX_n *(-1)
	lincom L1event_aX_n - L1event_bX_n *(-1)
	lincom L2event_aX_n - L2event_bX_n *(-1)	
	
	* total turnout
	estimates restore turnout_tot_req	
	// all post periods
	lincom  (1/3)*(L0event_aX_n + L1event_aX_n + L2event_aX_n) - (1/3)*(L0event_bX_n + L1event_bX_n + L2event_bX_n)*(-1)
	// single post periods
	lincom L0event_aX_n - L0event_bX_n *(-1)
	lincom L1event_aX_n - L1event_bX_n *(-1)
	lincom L2event_aX_n - L2event_bX_n *(-1)	
	
